package com.server.ehcache;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.SearchAttribute;
import net.sf.ehcache.config.Searchable;
import net.sf.ehcache.search.Attribute;
import net.sf.ehcache.search.Query;
import net.sf.ehcache.search.Result;
import net.sf.ehcache.search.Results;
import net.sf.ehcache.search.attribute.DynamicAttributesExtractor;

public class Test {
	public static void main(String[] args) {
		CacheManager cacheManager = CacheManager.create("config/ehcache.xml");
		Cache cache = cacheManager.getCache("pojo");
		if (cache==null) {
			System.out.println("create pojo cache !");
			CacheConfiguration cacheConfiguration = cacheManager.getConfiguration().getDefaultCacheConfiguration().clone();
//			System.out.println(PojoCacheEventListenerFactory.class.getName());
			cacheConfiguration.cacheEventListenerFactory(new CacheConfiguration.CacheEventListenerFactoryConfiguration().className("com.server.ehcache.PojoCacheEventListenerFactory"));
			cacheConfiguration.setName("pojo");
			
			//指定Cache的Searchable对象。
			Searchable searchable = new Searchable();
//			SearchAttribute searchAttribute = new SearchAttribute();
//			searchAttribute.setName("nameindex");
//			searchAttribute.setExpression("value.getName()");
//			searchable.addSearchAttribute(searchAttribute);
			
			searchable.allowDynamicIndexing(true);
			cacheConfiguration.searchable(searchable);
			
			
			cache = new Cache(cacheConfiguration);
			
			final String attrNames[] = {"name"};
			cache.registerDynamicAttributesExtractor(new DynamicAttributesExtractor() {
							
							@Override
							public  Map<String, Object> attributesFor(Element element) {
								Map<String, Object> attrs = new HashMap<String, Object>();
								Pojo value = (Pojo)element.getObjectValue();
								attrs.put(attrNames[0], value.getName());
								return attrs;
							}
						});
			
			cacheManager.addCache(cache);
		}
		
		cache = cacheManager.getCache("pojo");
		Element element = null;
		Pojo pojo = null;
//		List<Pojo> pojos = new ArrayList<>();
		for (int i = 1; i <= 1000000; i++) {
			pojo = new Pojo();
			pojo.setId(i);
			pojo.setName("name_" + i);
			pojo.setPassword("password" + i);
			pojo.setAge(10);
			pojo.setCreateDate(new Date());
			element = new Element(pojo.getId(), pojo);
			cache.put(element);
//			pojos.add(pojo);
		}
//		for (int i = 0; i < 10; i++) {
//		long t1 = System.currentTimeMillis();
//		Collection<Pojo> newPojos = Collections2.filter(pojos, new Predicate<Pojo>() {
//
//			@Override
//			public boolean apply(Pojo arg0) {
//				if (arg0.getName().equals("name_7785")) {
//					return true;
//				}
//				return false;
//			}
//		});
//		for (Pojo pojo2 : newPojos) {
//			System.out.println(pojo2);
//		}
//		long t2 = System.currentTimeMillis();
//        System.out.println(t2-t1);
//		}
		for (int i = 0; i < 10; i++) {
			long t1 = System.currentTimeMillis();
			Query query = cache.createQuery();
			//取得属性
	        Attribute<String> name = cache.getSearchAttribute("name");
	        //可以任务组合查询条件
	        query.addCriteria(name.eq("name_7785"));
	        //根据查询的条件 引入结果集
	        query.includeValues().end();
	        Results results = query.execute();
	        List<Result> list = results.all();
	        long t2 = System.currentTimeMillis();
	        System.out.println(t2-t1);
	        for (Result result : list) {
	            System.out.println(result.getValue());
	        }
		}
		
        
//        Pojo p = (Pojo) cache.get(5).getObjectValue();
//        p.setName("name_10");
//        
//        
//        query = cache.createQuery();
//        //可以任务组合查询条件
//        query.addCriteria(name.eq("name_5"));
//        
//        //根据查询的条件 引入结果集
//        query.includeValues().end();
//        
//        results = query.execute();
//        
//        list = results.all();
//        
//        for (Result result : list) {
//            System.out.println(result.getValue());
//        }
//        
        
	}
}
